package com.flexandair.view
{
	import com.flexandair.ApplicationFacade;
	import com.flexandair.templates.CodeFactory;
	import com.flexandair.templates.ServerProfileTemplate;
	import com.flexandair.view.components.ServerProperties;
	
	import flash.data.EncryptedLocalStore;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	
	import mx.events.FlexEvent;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class ServerPropertiesMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "serverPropertiesMediator";
		
		private var profileArray:Array = new Array();
		private var preferencesFile:File = File.applicationStorageDirectory.resolvePath('servers.cbobj');
		private var codeFactory:CodeFactory = CodeFactory.getInstance();
		
		public function ServerPropertiesMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
			this.serverProperties.addEventListener(FlexEvent.CREATION_COMPLETE, onServerPropertiesCreationComplete);
		}
		
		override public function getMediatorName():String
		{
			return NAME;
		}
		
		override public function getViewComponent():Object
		{
			return null;
		}
		
		override public function setViewComponent(viewComponent:Object):void
		{
		}
		
		override public function listNotificationInterests():Array
		{
			return [];
		}
		
		override public function handleNotification(notification:INotification):void
		{
		}
		
		private function onServerPropertiesCreationComplete( event:FlexEvent ) : void
		{

		}
		private function initializeView() : void
		{
			var fileStream:FileStream = new FileStream();
			if ( preferencesFile.exists )
			{
				fileStream.open(preferencesFile, FileMode.READ);
				profileArray = fileStream.readObject();
				fileStream.close();
				
				this.serverProperties.profileSelector.dataProvider = profileArray;
			//	populateForm(profileArray[0]);
			}

		}
		
		private function onSaveTriggerFire( event:MouseEvent ) : void
		{
			var profile:ServerProfileTemplate = new ServerProfileTemplate();
			profile.dbHost = this.serverProperties.dbHostInput.text;
			profile.dbUser = this.serverProperties.dbUserInput.text;
			profile.dbName = this.serverProperties.dbNameInput.text;
			profile.profileName = this.serverProperties.profileNameInput.text;
			//profile.webURL = webURLInput.text;
			var pass:ByteArray = new ByteArray();
			pass.writeUTFBytes(this.serverProperties.dbPassInput.text);
			EncryptedLocalStore.setItem(profile.profileName + 'password', pass);
			profileArray.push(profile);
			var fileStream:FileStream = new FileStream();
			fileStream.open(preferencesFile, FileMode.WRITE);
			fileStream.writeObject(profileArray);
			fileStream.close();
			
			initializeView();

		}
		
		private function onProfileSelectorChange( event:Event ) : void
		{
			var profile:Object = event.currentTarget.selectedItem;
 			serverProperties.dbHostInput.text = profile.dbHost;
			serverProperties.dbUserInput.text = profile.dbUser;
			serverProperties.dbNameInput.text = profile.dbName;
			serverProperties.profileNameInput.text = profile.profileName;
			//webURLInput.text = cfInstance.webURL;
			var byteArray:ByteArray = EncryptedLocalStore.getItem(profile.profileName + 'password');
			if ( byteArray != null )
			{
				codeFactory.dbPass = byteArray.readUTFBytes(byteArray.bytesAvailable);
				serverProperties.dbPassInput.text = codeFactory.dbPass;
			}

		}
		
		private function onChooseProfileTriggerFire ( event:MouseEvent ) : void
		{
 			codeFactory.dbHost = this.serverProperties.profileSelector.selectedItem.dbHost;
			codeFactory.dbUser = this.serverProperties.profileSelector.selectedItem.dbUser;
			codeFactory.webURL = this.serverProperties.profileSelector.selectedItem.webURL;
			codeFactory.dbName = this.serverProperties.profileSelector.selectedItem.dbName;
			
			serverProperties.dbHostInput.text = codeFactory.dbHost;
			serverProperties.dbUserInput.text = codeFactory.dbUser;
			serverProperties.dbNameInput.text = codeFactory.dbName;
			serverProperties.profileNameInput.text = this.serverProperties.profileSelector.selectedItem.profileName;
			//TODO: determine what the below is used for 
			//webURLInput.text = cfInstance.webURL;
			var byteArray:ByteArray = EncryptedLocalStore.getItem( this.serverProperties.profileSelector.selectedItem.profileName + 'password' );
			if( byteArray != null )
			{
				codeFactory.dbPass = byteArray.readUTFBytes( byteArray.bytesAvailable );
				this.serverProperties.dbPassInput.text = codeFactory.dbPass;
			}
			sendNotification(ApplicationFacade.DATABASE_PROFILE_SELECTED);
			
		}
		
		override public function onRegister():void
		{
			initializeView();
			this.serverProperties.saveTrigger.addEventListener( MouseEvent.CLICK, onSaveTriggerFire );
			this.serverProperties.profileSelector.addEventListener( Event.CHANGE, onProfileSelectorChange );
			this.serverProperties.chooseProfileTrigger.addEventListener( MouseEvent.CLICK, onChooseProfileTriggerFire );
		}
		
		override public function onRemove():void
		{
		}
		
		public function get serverProperties () : ServerProperties
		{
			return viewComponent as ServerProperties;
		}
		
	}
}